package com.common.playing.apps.comm.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.kankan.wheel.widget.OnWheelChangedListener;
import com.common.kankan.wheel.widget.OnWheelScrollListener;
import com.common.kankan.wheel.widget.WheelView;
import com.common.kankan.wheel.widget.adapters.NumericWheelAdapter;
import com.common.kankan.wheel.widget.adapters.WheelViewAdapter;
import com.common.playing.apps.comm.listener.OnCyclePickerChangeListener;

import width.hgk.wanwan.R;


/**
 * 日期选择控件
 * <p>
 * 日期控件的数据适配器请使用DateNumericAdapter
 * 
 * @author pengxh
 * 
 */
public class CyclePickerView extends LinearLayout {

	private final String TAG = "CyclePickerView";

	private String dateFormat = "月经周期：%d天";

	private WheelView cycleWheelView;

	private Button btnCancel;
	private Button btnSure;
	private TextView tvShowLabel;

	private View mLabelBar;

	private Context mContext;

	private CyclePickerView mCyclePicker;

	private OnCyclePickerChangeListener onCyclePickerChangeListener;

	private WheelViewAdapter dayAdapter;

	public CyclePickerView(Context context) {
		super(context);
		mContext = context;
	}

	public CyclePickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		init(mContext);
	}

	/**
	 * 初始化
	 * 
	 * @param context
	 */
	private void init(Context context) {

		mCyclePicker = this;

		cycleWheelView = (WheelView) findViewById(R.id.day);

		mLabelBar = findViewById(R.id.operatorLaberBar);

		btnCancel = (Button) findViewById(R.id.btnCancelDatePicker);
		btnSure = (Button) findViewById(R.id.btnSureDatePicker);
		tvShowLabel = (TextView) findViewById(R.id.tvShowTimeDatePicker);

		// Cycle 默认设置为循环
		cycleWheelView.setCyclic(true);

		// 添加时间变动监听
		cycleWheelView.addChangingListener(onWheelChangeListener);

		// 添加滚动监听
		cycleWheelView.addScrollingListener(onWheelScrollListener);

		// 设置可见items条数
		setVisibeItems(5);

	}

	/**
	 * 显示当前最新周期到 Lable
	 * <p>
	 * 获取最新周期天数
	 */
	private int obtainCurrentCycle() {
		int minDay = getViewAdapter(cycleWheelView).getMinValue();
		int currDay = minDay + cycleWheelView.getCurrentItem();

		showLable(currDay);

		return currDay;
	}

	/**
	 * 监听日期控件时间变动
	 * <p>
	 * 周期
	 */
	private OnWheelChangedListener onWheelChangeListener = new OnWheelChangedListener() {
		public void onChanged(WheelView wheel, int oldValue, int newValue) {

			obtainCurrentCycle();

			if (null != onCyclePickerChangeListener) {
				onCyclePickerChangeListener.onChanging(mCyclePicker, obtainCurrentCycle());
			}
		}
	};

	/**
	 * 监听控件的滚动
	 * <p>
	 * 周期
	 */
	private OnWheelScrollListener onWheelScrollListener = new OnWheelScrollListener() {

		@Override
		public void onScrollingStarted(WheelView wheel) {
			if (null != onCyclePickerChangeListener) {
				onCyclePickerChangeListener.onChangeStart(mCyclePicker, obtainCurrentCycle());
			}
		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			if (null != onCyclePickerChangeListener) {
				onCyclePickerChangeListener.onChangeEnd(mCyclePicker, obtainCurrentCycle());
			}
		}
	};

	/**
	 * 显示周期
	 * 
	 */
	private void showLable(int days) {
		tvShowLabel.setText(String.format(dateFormat, days));
	}

	// -------------------------------API----------------------

	/**
	 * 获取适配器
	 */
	public NumericWheelAdapter getViewAdapter(WheelView wheelView) {
		if (null != wheelView) {
			return (NumericWheelAdapter) wheelView.getViewAdapter();
		}
		return null;
	}

	/**
	 * 获取周期的WheelView
	 * 
	 * @return
	 */
	public WheelView getWheelView() {
		return cycleWheelView;
	}

	/**
	 * 设置周期显示格式(月经周期：%d天)
	 * 
	 * @param fomat
	 */
	@SuppressLint("SimpleDateFormat")
	public void setLabelFormat(String fomat) {
		if (!TextUtils.isEmpty(fomat)) {
			this.dateFormat = fomat;
		}
	}

	/**
	 * 添加周期选择控件时间变动监听
	 * 
	 * @param onCyclePickerChangeListener
	 */
	public void setCyclePickerChangeListener(OnCyclePickerChangeListener onCyclePickerChangeListener) {
		if (null != onCyclePickerChangeListener) {
			this.onCyclePickerChangeListener = onCyclePickerChangeListener;
		}
	}

	/**
	 * 设置取消按钮监听
	 * 
	 */
	public void setNegativeListener(OnClickListener onNegativeClickListener) {
		btnCancel.setOnClickListener(onNegativeClickListener);
	}

	/**
	 * 设置确定按钮监听
	 *
	 * @param onPositiveClickListener
	 */
	public void setPositiveListener(OnClickListener onPositiveClickListener) {
		btnSure.setOnClickListener(onPositiveClickListener);
	}

	/**
	 * 设置周期循环
	 * 
	 * @param isCyclic
	 */
	public void setCyclic(boolean isCyclic) {
		cycleWheelView.setCyclic(isCyclic);
	}

	/**
	 * 设置可见items的条数
	 * 
	 * @param counts
	 */
	public void setVisibeItems(int counts) {
		if (counts > 0) {
			cycleWheelView.setVisibleItems(counts);
		}
	}

	/**
	 * 显示顶部标签、操作栏
	 */
	public void showLabelBar() {
		mLabelBar.setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏顶部标签、操作栏
	 */
	public void hideLabelBar() {
		mLabelBar.setVisibility(View.GONE);
	}

	/**
	 * 获取左侧取消按钮
	 * 
	 * @return
	 */
	public Button getNegativeButton() {
		return btnCancel;
	}

	/**
	 * 获取右侧确定按钮
	 * @return
	 */
	public Button getPositiveButton() {
		return btnSure;
	}

	/**
	 * 获取周期显示Label TextView
	 * @return
	 */
	public TextView getLabel() {
		return tvShowLabel;
	}

	/**
	 * 设置周期Label字体颜色
	 * @param color
	 */
	public void setLabelTextColor(int color) {
		tvShowLabel.setTextColor(color);
	}

	/**
	 * 设置周期Label字体大小
	 * @param size
	 */
	public void setLabelTextSize(float size) {
		tvShowLabel.setTextSize(size);
	}

	/**
	 * 初始化适配器&刷新适配器
	 */
	public void initAdapter(WheelViewAdapter dayAdapter) {

		this.dayAdapter = dayAdapter;

		cycleWheelView.setViewAdapter(dayAdapter);
	}

	/**
	 * 刷新到需要显示的周期天数
	 * 
	 * @param cycleDay
	 */
	public void refreshItemsScrollTo(int cycleDay) {
		int min = getViewAdapter(cycleWheelView).getMinValue();
		int max = getViewAdapter(cycleWheelView).getMaxValue();
		if (cycleDay < min || cycleDay > max) {
			throw new IllegalArgumentException("需要跳转的月经周期天数有误...");
		} else {
			cycleWheelView.setCurrentItem(cycleDay - min);
		}
	}
}
